package com.impinj.program;

import java.util.ArrayList;

import com.impinj.octane.AntennaConfigGroup;
import com.impinj.octane.BitPointers;
import com.impinj.octane.ImpinjReader;
import com.impinj.octane.MemoryBank;
import com.impinj.octane.OctaneSdkException;
import com.impinj.octane.SequenceState;
import com.impinj.octane.Settings;
import com.impinj.octane.TagData;
import com.impinj.octane.TagOp;
import com.impinj.octane.TagOpSequence;
import com.impinj.octane.TagReadOp;
import com.impinj.octane.TagReadOpResult;
import com.impinj.octane.TagWriteOp;
import com.impinj.octane.TargetTag;
import com.impinj.octane.TagOpCompleteListener;
import com.impinj.octane.TagOpReport;

public class MainWrite {
    private static int closeTimeCnt = 0;
    private static int endTimes = 10;
    public static void main(String[] args) {
        if(args.length != 1){
            System.out.println("输入参数错误，正确方式为：java -jar *.jar 612B0001FFFFFFFF00010000");
            System.exit(0);
        }
        ImpinjReader reader = new ImpinjReader();
        try{
            String hostname = InitializeReader.getHostname();
            if (!hostname.isEmpty()) {
                if (InitializeReader.connectToReader(reader, hostname)) {	//读写器连接成功
                    System.out.println("》》》读写器初始化完成《《《");
                }
                else {
                    System.out.println("请检查读写器的连接，程序将退出");
                    System.exit(0);
                }
            } 

            // Get the default settings
            Settings settings = reader.queryDefaultSettings();

            settings.getReport().setIncludeAntennaPortNumber(true);

            // Apply the new settings
            reader.applySettings(settings);
            
            
            System.out.println("创建读写动作流");
            // create the reader op sequence
            TagOpSequence seq = new TagOpSequence();
            seq.setOps(new ArrayList<TagOp>());
            seq.setExecutionCount((short) 0); // forever
            seq.setState(SequenceState.Active);
            seq.setId(1);

            TagWriteOp writeOp = new TagWriteOp();
            writeOp.setMemoryBank(MemoryBank.User);
            writeOp.setWordPointer((short) 0);
            // 这里是要写入的内容示例，总共6个4位16进制数据，第一个固定为房间号
            String writein = args[0];//"612B 0001 FFFF FFFF 0001 0000";
            writeOp.setData(TagData.fromHexString(writein));

            // add to the list
            seq.getOps().add(writeOp);

            // Use target tag to only apply to some EPCs
            String targetEpc = System.getProperty(SampleProperties.targetTag);

            if (targetEpc != null) {
                seq.setTargetTag(new TargetTag());
                seq.getTargetTag().setBitPointer(BitPointers.Epc);
                seq.getTargetTag().setMemoryBank(MemoryBank.Epc);
                seq.getTargetTag().setData(targetEpc);
            } else {
                // or just send NULL to apply to all tags
                seq.setTargetTag(null);
            }

            System.out.println("设置动作到读写器");
            // add to the reader. The reader supports multiple sequences
            reader.addOpSequence(seq);

            // set up listeners to hear stuff back from SDK. Normally the 
            // application would enable this but we dont want too many 
            // reports in our example
            // reader.setTagReportListener(new TagReportListenerImplementation());

            reader.setTagOpCompleteListener(new TagOpCompleteListener() {
                @Override
                public void onTagOpComplete(ImpinjReader reader, TagOpReport results) {
                    ++closeTimeCnt;
                    System.out.println("count down "+ String.valueOf(endTimes-closeTimeCnt));
                    if(closeTimeCnt >= endTimes){
                        System.out.println("写入完成");
                        try {
                            reader.stop();
                        } catch (OctaneSdkException e) {
                            System.out.println("程序运行可能存在问题 : " + e);
                        }
                        reader.disconnect();   
                        System.exit(0);     
                    }
                    else{}
                }
            } );

            // Start the reader
            System.out.println("开始写入");
            reader.start();

              
        }
        catch (OctaneSdkException ex) {
            System.out.println("Octane SDK exception : " + ex.getMessage());
            try {
                reader.stop();
                reader.disconnect();
            } catch (OctaneSdkException e) {
                e.printStackTrace();
            }
        }
    }
    
}
